package com.xixing.weixin.cmd;

import com.xixing.weixin.exception.AccountException;
import com.xixing.weixin.exception.ArgumentException;
import com.xixing.weixin.exception.WeixinExecuteException;
import com.xixing.weixin.domain.Account;
import com.xixing.weixin.domain.Weixin;
import com.xixing.weixin.response.Response;
import com.xixing.weixin.response.TextResponse;
import com.xixing.weixin.utils.GzhuSchoolUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.dao.EmptyResultDataAccessException;


public class ScoreExecutor extends AbstractExecutor {

    public static class Arg {
        public Arg() {
        }

        public boolean isDefault;
        public Integer year;
        public String username;
        public String password;
        public Integer startYear;

        @Override
        public String toString() {
            return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        }
    }


    @Override
    public Response execute(Weixin weixin, Object args) throws WeixinExecuteException {
        if (!(args instanceof Arg)) throw new ArgumentException();
        Arg arg = (Arg) args;
        if (arg.year == null) throw new ArgumentException();
        if(arg.startYear == null) throw new ArgumentException();
        if (StringUtils.isBlank(arg.username)) throw new AccountException(Account.GZHU_SCHOOL);
        if (StringUtils.isBlank(arg.password)) throw new AccountException(Account.GZHU_SCHOOL);

        try {
            String html = GzhuSchoolUtils.findScore(arg.username, arg.password, Account.findStudentYear(arg.startYear, arg.year));
            String pga = GzhuSchoolUtils.parseScore(html);
            if(arg.isDefault) {
                pga = pga + getHelp();
            }
            return TextResponse.create(weixin, pga);
        } catch (Exception e) {
            throw new ArgumentException();
        }

    }

    @Override
    public Object parseArgs(Weixin weixin, Object args) throws WeixinExecuteException {

        assertWeixinMsgType(weixin, Weixin.TEXT);
        assertWeixinContentLength(weixin, 1, 2);

        Arg arg = new Arg();
        try {
            Account account = Account.findAccountsByWeixinCodeAndAccountType(weixin.getFromUserName(), Account.GZHU_SCHOOL).getSingleResult();
            arg.username = account.getStudentCode();
            arg.password = account.getStudentPassword();
            arg.startYear = account.getStartYear();
            arg.year = account.findYear();
            arg.isDefault = true;
        } catch (EmptyResultDataAccessException e) {
            throw new AccountException(Account.GZHU_SCHOOL);
        }

        String[] contentArgs = AbstractExecutor.splitCommand(weixin.getContent());
        String temp = AbstractExecutor.normalCommand(contentArgs[0]);
        if (isCommand(temp)) {
            if (contentArgs.length > 1) {
                arg.year = toArg(contentArgs[1]);
                arg.isDefault = false;
            }
        } else {
            arg.year = toArg(contentArgs[0]);
            arg.isDefault = false;
        }
        return arg;
    }

    private Integer toArg(String contentArg) {
        // TODO : 处理一、二，以及大一、大二
        if (StringUtils.isNumeric(contentArg)) {
            return Integer.parseInt(contentArg);
        }
        return null;
    }
}
